-------------------------------------------------------------------------------
-- This file is loaded at the beginning of each file to check a existe session
-- If existe, all of the session's data is loaded in the global table "sessionData"
-- If not, sessionData = "nil"
-------------------------------------------------------------------------------

require"cgilua.cookies"
require"cgilua.session"
local lfs = require"lfs"
local io = require("io")

dofile("common.lua")

-- Get current sessionID from cookies
local sessionID = cgilua.cookies.get("sessionID")

-- Set necessary parametres for cgilua.session
cgilua.session.setsessiondir(getParameter("WEB_SESSION_DIR"))
cgilua.session.setsessiontimeout(tonumber(getParameter("WEB_SESSION_TIME_OUT")))

--local sessionTable = {}
--local dir = lfs.dir

--for sessionFile in dir (getSessionDir()) do
--	local start, _ = string.find(sessionFile, ".lua")
--	if (start ~= nil) then
--		sessionFile = string.sub(sessionFile, 1, start - 1)
--		table.insert(sessionTable, sessionFile)
--	end
--end

cgilua.session.open()

--for sessionFile in dir (getSessionDir()) do
--	for index = 1, #sessionTable do
--		if (tostring(sessionTable[index]..".lua") == tostring(sessionFile)) then
--			table.remove(sessionTable, index)
--		end
--	end 
--end

-- Connect to Master Peer of each expired session to delete them
socket = require("socket")
local connectMasterPeer = socket.tcp()

--local mysqlParameters = getMysqlParametersFromConfFile()
--local env = assert (luasql.mysql())
--local connectData = assert (env:connect(mysqlParameters.database, mysqlParameters.user, mysqlParameters.password, mysqlParameters.hostID, mysqlParameters.port))
--for index = 1, #sessionTable do
--	local exeCurSession = connectData:execute(string.format([[SELECT * FROM session WHERE sessionID = '%d']], tonumber(sessionTable[index])))
--	rowSession = exeCurSession:fetch ({}, "a")
	
	-- Connect to Master_Peer of the expired sessions
--	connectMasterPeer:connect(tostring(row.IPMP), getParameter("SPLAYD_PORT"))
--	connectMasterPeer:send("EXPIREDSESSION\n") -- Message Code
--	connectMasterPeer:send(tostring(row.userID).."\n")
--	local exeDeleteExpiredSession = connectData:execute(string.format([[DELETE FROM session WHERE sessionID = '%d']], tonumber(sessionTable[index])))
--end

-- Delete the expired session from Access Peer
local executionStr = string.format([[SELECT * FROM session]])
local execution = db_Execute (executionStr)
rowSelect = execution:fetch ({}, "a")
while (rowSelect) do
	if (rowSelect.timeStart + tonumber(getParameter("WEB_SESSION_TIME_OUT")) < tonumber(os.time())) then
		executionStr = string.format([[DELETE FROM session WHERE ID = '%d']], tonumber(rowSelect.ID))
		local execution1 = db_Execute (executionStr)
	end
	rowSelect = execution:fetch ({}, "a")
end

--connectData:close()
--env:close()

-- Get datas of current session base on sessionID
sessionData = {}
sessionData = cgilua.session.load(tonumber(sessionID))

cgilua.session.close ()

-- If exist one session then show it in the browser
if (sessionData ~= nil) then 
cgilua.put([[<small><div align=right>]])
cgilua.put(sessionData["email"])
cgilua.put([[<a href="vote.lp">&nbsp;|&nbsp; Vote </a>]])
cgilua.put([[<a href="http://128.178.155.92:9999/voteTable.lp" target="_blank">&nbsp;|&nbsp; VoteTable </a>]])
cgilua.put([[<a href="changePassword.lp">&nbsp;|&nbsp; Change Password </a>]])
cgilua.put([[<a href="logout.lp">&nbsp;|&nbsp; Logout </a>]])
cgilua.put([[</div></small>]])
end